<?php

namespace App\Service\Activity;

use App\Helpers\AliyunUtil;
use App\Model\GiftLog;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 圣诞袜活动
 */
class ChristmasService extends BaseService
{
    public function handle()
    {
        $profileId = $this->request->input('profile_id', '');
        $act = $this->request->input('act', '');
        $selfProfile = !$profileId ? null : UserProfile::getInfoByProfileId($profileId);
        $userId = $selfProfile ? $selfProfile->user_id : 0;
//        $ret['list'] = [];
        $giftId = $act == 'yuandan' ? [172] : [40, 48];
        $time = $act == 'yuandan' ? ['2022-12-23', '2022-12-29']  : ['2021-12-23', '2022-01-01'];
        $rankInfo = GiftLog::giftRankList($giftId, 20, $userId, $time[0], $time[1]);
        $userIds = array_column($rankInfo['rank_list'], 'user_id');
        $userProfiles = UserProfile::getUsersProfile($userIds, false, true);
        $ret['list'] = [];
        foreach ($userProfiles as $k => $profile) {
            if (!UserProfile::checkRoleHost($profile['role'])) continue;
            $ret['list'][] = [
                'rank' => $k + 1,
                'name' => $profile['nick_name'],
                'num' => $rankInfo['rank_list'][$k]['num'],
                'img' => UserProfile::buildAvatar($profile['avatar'], 150),
            ];
        }
        $ret['list'] = array_slice($ret['list'], 0, 10);
        if (!$profileId) {
            $ret['my_info'] = [
                'rank' => 0,
                'name' => '昵称',
                'num' =>  0,
                'img' => AliyunUtil::IMG_HOST . 'default/ma_avatar.jpeg',
                'gender' => 1,
                'role' => 1
            ];
        } else {
            $ret['my_info'] = [
                'rank' => $rankInfo['my_rank'],
                'name' => $selfProfile->nick_name,
                'num' => $rankInfo['my_num'],
                'img' => UserProfile::buildAvatar($selfProfile->avatar, 150),
                'gender' => $selfProfile->gender,
                'role' => $selfProfile->role,
            ];
        }

        if (count($ret['list']) >= 2) {
            $tmp = $ret['list'][1];
            $ret['list'][1] = $ret['list'][0];
            $ret['list'][0] = $tmp;
        }
        $ret['act_time'] = $time;
        return $ret;
    }
}
